package com.blog.Bard.base.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.Bard.base.pojo.BardArticleType;
import com.blog.Bard.base.service.BardArticleTypeService;
import com.blog.Bard.common.controller.BaseController;
import com.blog.Bard.common.util.Pager;

/**
 * 	文章分类
 * @author phil
 *
 */
@Controller
@RequestMapping("/base/articleType")
public class BardArticleTypeController extends BaseController {

	@Autowired
	private BardArticleTypeService bardArticleTypeService;
	
	/**
	 * 	列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model) {
		logger.info("execute the ArticleTypeController 's list method...");
		return "articleType/list";
	}
	
	/**
	 * 文章分类分页
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/pageList")
	public Map<String,Object> pageList(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the ArticleTypeController 's pageList method...");
		Map<String,Object> result = new HashMap<String, Object>();
		Map<String,Object> params = new HashMap<String, Object>();
		Pager data = null;
		try {
			Integer pageNo = super.getInt(req, "page", 1);
			Integer pagesize = super.getInt(req, "limit", 10);
			
			params.put("pageNo", pageNo);
			params.put("pagesize", pagesize);
			
			data = bardArticleTypeService.pageList(params);
			
			result = super.getSuccessResultMap("ok", data.getData(), data.getTotalRecord());
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	
	/**
	 * 新增/编辑页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest req, HttpServletResponse res, Model model) {
		logger.info("execute the ArticleTypeController 's addPage method...");
		BardArticleType t = new BardArticleType();
		try {
			Integer id = super.getInt(req, "id");
			if(id>0) {
				t = bardArticleTypeService.getOne(id);
				model.addAttribute("id", id);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("entity", t);
		return "articleType/addPage";
	}
	
	/**
	 *  字典保存
	 * @param req
	 * @param res
	 * @param t
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Map<String,Object> save(HttpServletRequest req, HttpServletResponse res, BardArticleType t) {
		logger.info("execute the ArticleTypeController 's save method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			bardArticleTypeService.save(t);
			result = super.getSuccessResultMap("ok", null);
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 	删除
	 * @param req
	 * @param res
	 * @param t
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Map<String,Object> del(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the ArticleTypeController 's del method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Integer id = super.getInt(req, "id", 0);
			if(id==0) {
				result = super.getErrorResultMap("fail");
			}else {
				bardArticleTypeService.del(id);
				result = super.getSuccessResultMap("ok", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}

	
}
